/*
 * GPSPanel.java
 *
 */
package robotour.navi.gps;

import robotour.navi.gps.input.GPSInput;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Arrays;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JPanel;

/**
 *
 * @author  PC
 */
public class GPSPanel extends javax.swing.JPanel {

    private GPSInput gps;

    /** Creates new form GPSPanel
     * @param gpsinput 
     */
    public GPSPanel(GPSInput gpsinput) {
        this.gps = gpsinput;
        initComponents();
        new Timer().schedule(new TimerTask() {

            @Override
            public void run() {
                repaint();
            }

        }, 100, 100);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        satelitesPanel = new SatelitesPanel();
        jPanel1 = new javax.swing.JPanel();
        latitude = new javax.swing.JLabel();
        longitude = new javax.swing.JLabel();
        altitude = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        channels = new javax.swing.JLabel();
        azimuth = new javax.swing.JLabel();
        speed = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        precision = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(204, 255, 204));

        javax.swing.GroupLayout satelitesPanelLayout = new javax.swing.GroupLayout(satelitesPanel);
        satelitesPanel.setLayout(satelitesPanelLayout);
        satelitesPanelLayout.setHorizontalGroup(
            satelitesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 288, Short.MAX_VALUE)
        );
        satelitesPanelLayout.setVerticalGroup(
            satelitesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 225, Short.MAX_VALUE)
        );

        latitude.setText("latitude");

        longitude.setText("longitude");

        altitude.setText("altitude");

        jLabel1.setText("latitude");

        jLabel2.setText("longitude");

        jLabel3.setText("altitude");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(latitude)
                    .addComponent(jLabel1))
                .addGap(38, 38, 38)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(longitude)
                    .addComponent(jLabel2))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addComponent(jLabel3)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(altitude)
                        .addGap(18, 18, 18))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(latitude)
                    .addComponent(longitude)
                    .addComponent(altitude))
                .addContainerGap())
        );

        channels.setText("channels");

        azimuth.setText("azimuth");

        speed.setText("speed");

        jLabel4.setText("speed");

        jLabel5.setText("azimuth");

        precision.setText("precision");

        jLabel6.setText("precision");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(channels, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                        .addComponent(precision))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING))
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                                .addComponent(azimuth))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(19, 19, 19)
                                .addComponent(speed)))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(speed))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(azimuth))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(precision))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(channels)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(satelitesPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(satelitesPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(64, 64, 64))))
        );
    }// </editor-fold>//GEN-END:initComponents
    private int[] satelitesInt;
    private List<GPSInput.Satellite> satellites;

    public class SatelitesPanel extends JPanel {

        SatelitesPanel() {

            setBackground(Color.YELLOW);
            repaint();

        }

        @Override
        public void paint(Graphics g) {

            int width = getWidth();
            int height = getHeight();

            g.setColor(Color.BLACK);

            g.fillRect(0, 0, width, height);


            g.setColor(Color.WHITE);

            int R = 220;

            g.drawOval(width / 2 - R, height / 2 - R, 2 * R, 2 * R);
            g.drawLine(width / 2 - R, height / 2 - R, width / 2 + R, height / 2 + R);
            g.drawLine(width / 2 - R, height / 2, width / 2 + R, height / 2);
            g.drawLine(width / 2, height / 2 - R, width / 2, height / 2 + R);

            g.drawString(Arrays.toString(satelitesInt), 20, 20);

//            for (int i = 0; i < satelitesInt.length; i += 4) {
//                if (satelitesInt[i] > 0) {
//                    double r = Math.sin(Math.toRadians(satelitesInt[i + 1])) * R;
//                    g.drawString("S" + satelitesInt[i], width / 2 + (int) (r * Math.sin(Math.toRadians(satelitesInt[i + 2]))), height / 2 - (int) (r * Math.cos(Math.toRadians(satelitesInt[i + 2]))));
//
//                    g.drawString("" + satelitesInt[i], 5 * i + 4, height - 5);
//                    g.drawRect(5 * i + 2, height - 120, 15, 100);
//                    g.fillRect(5 * i + 2, height - 120 + satelitesInt[i + 3], 15, 100 - satelitesInt[i + 3]);
//                }
//            }
            int i = 0;
            for (GPSInput.Satellite satellite : satellites) {
                double r = Math.sin(Math.toRadians(satellite.getAzimuth())) * R;
                g.drawString("S" + satellite.getId(),
                        width / 2 + (int) (r * Math.sin(Math.toRadians(satellite.getElevation()))),
                        height / 2 - (int) (r * Math.cos(Math.toRadians(satellite.getElevation()))));
                g.drawString("" + satellite.getId(), 20 * i + 4, height - 5);
                g.drawRect(20 * i + 2, height - 120, 15, 100);
                g.fillRect(20 * i + 2, height - 120 + satellite.getSignal(), 15, 100 - satellite.getSignal());
                i++;
            }

            if (gps.isValid()) {
                g.setColor(Color.RED);
                g.drawString("no signal", width / 2, height / 2);
            }
        }
    }

    @Override
    public void repaint() {
        if (gps != null) {

            this.satelitesInt = gps.getSatelitesInt();
            this.satellites = gps.getSatellites();


//            latitude.setText(gps.getPosition().latitude().toString());
//            longitude.setText(gps.getPosition().longitude().toString());
//
//            speed.setText(gps.getSpeed() + " m/s");
//            azimuth.setText(gps.getAzimuth() + " deg");
//
//            altitude.setText(gps.getAltitude() + " mnm");
//
//            channels.setText(gps.getNumChannels() + " chs");
//
//            precision.setText(gps.getPrecision() + " m");

        }
        super.repaint();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    javax.swing.JLabel altitude;
    javax.swing.JLabel azimuth;
    javax.swing.JLabel channels;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    javax.swing.JLabel latitude;
    javax.swing.JLabel longitude;
    javax.swing.JLabel precision;
    javax.swing.JPanel satelitesPanel;
    javax.swing.JLabel speed;
    // End of variables declaration//GEN-END:variables
}
